var that,
  api = new(require('../../utils/api.js')),
  util = new(require('../../utils/util.js')),
  imghead = api.getimgurl(),
  imghead = 'https://www.jihemu.com/bookapi/imgs/',
  videohead='https://www.jihemu.com/bookapi/myvideo/',
  audiohead='https://www.jihemu.com/bookapi/myaudio/',
  myaudio=wx.createInnerAudioContext(),
  app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    videohead,
    days:'',
    imghead,
    flag: !0,
    list: [],
    total: 0,
    pagesize: 5,
    start: 0,
    newcomment: '',
    nickName: '',
    avatarUrl: '',
    commentarr: [],
    isplay:!1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this;
    let {id}=options
    that.setData({id})
    console.log(options,'optionsoptionsoptionsoptionsoptions');
    
    if (app.logininfo) {
      let cid=options.id;
      that.setData({
        cid
      },()=>{
        that.getdata();
      })
    } else {
      util.setwatcher(app, this.afterlogin)
    }
    
    myaudio.onPlay(()=>{
      console.log('正在播放语音')
    })
    myaudio.onStop(()=>{
      console.log('语音被停止');
      myaudio.src='a'
    })
    myaudio.onEnded(()=>{
      console.log('语音自然播放结束');
      myaudio.src='a'
      that.setData({isplay:!1})
    })
    
    },

 
    afterlogin: {
      token: res => {
        that.getdata();
      }
    },


  /**
   * 获取数据
   */
  getdata() {
    wx.showLoading({
      title: '加载中',
    })
    let {
      pagesize,
      start,
      
    } = that.data;
    api.resp_mp_getlist({
      cid:that.data.id,
      pagesize,
      start
    }, res => {
      wx.hideLoading()
      console.log(res,'dfbbfhbfhbdhgb');
      if (res.data.issuccess == 1) {
        let {
          total,
          list
        } = res.data;
        list.map((x) => {
          x.updateDate = util.myTime(x.updateDate)
        })
        let data = res.data.data, piclist=[];
        data.updateTime = util.myTime(data.updateTime),
          piclist = data.piclist.split(','),
          piclist.forEach((y, i) => {
            // x.piclist = piclist;
            piclist[i] = y.replace('imgs/','')
          });
          data.piclist=piclist;
        that.setData({
          total,
          list,
          data,
          days:res.data.days
        })
        console.log(data, 'mmlml')
      }
    })

  },

  //播放音频
  playaudio(e){
    console.log(e,'eeeeeee')
    let{src,duration,isplay}=e.currentTarget.dataset
    if(!isplay){
      that.setData({isplay:!0})
    }
      if(myaudio.src==audiohead+src){
        myaudio.stop()
        myaudio.play()
        return
      }
      myaudio.src=audiohead+src
      myaudio.play()
      
  },

  /**
   * input
   */
  input(e) {
    let  { newcomment } =that.data
    that.setData({
      newcomment: e.detail.value
    })
    console.log(newcomment,'newcommentnewcommentnewcomment')
  },


  /**
   * 图片放大事件
   */
  previewImg(e) {
    console.log(e.currentTarget.dataset.index);
    var index = e.currentTarget.dataset.index;
    var imgArr = this.data.data.piclist;
    wx.previewImage({
      current: imgArr[index], //当前图片地址
      urls: imgArr, //所有要预览的图片的地址集合 数组形式
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },


  modsend() {
    that.setData({
      sendshow: !1
    })
  },
  writecomment() {
    that.setData({
      sendshow: !0
    })
  },
  /**
   * 添加评论的按钮事件
   */
  addcomment() {
    let {
      newcomment,
      flag,
      data,
      cid
    } = that.data;
    //判断用户是否授权
    let userinfo = wx.getStorageSync("userinfo");
    if (!userinfo && flag) {
      that.setData({
        flag: 0
      })
      wx.navigateTo({
        url: '../empower/empower',
      })
      return
    }
    if (!!userinfo) {
      //获取用户信息
      wx.getUserInfo({
        success: res => {
          wx.showLoading({
            title: '加载中',
          })
          // console.log(res, '12334131s')
          that.setData({sendshow:!1})
          let {
            nickName,
            avatarUrl
          } = res.userInfo;
          let date = new Date();
          date = util.formatTime(date);
          //添加评论
          api.addcomment({
            username: nickName,
            avatarUrl,
            contents: newcomment,
            cid: data.id
          }, res => {
            wx.hideLoading()
            
            console.log(res, 'aabcc')
            if (res.data.issuccess == 1) {
              that.setData({ newcomment: '' },()=>{
               that.getdata(cid) //刷新列表
              })
            } else if (res.data.msg = "wrong 包含敏感内容") {
              wx.showToast({
                title: '您输入的评论包含敏感内容，请重新输入',
                icon: 'none'
              })
            }
          })
        }
      })
    } else {
      api.addcomment({
        username: '匿名用户',
        avatarUrl: '',
        contents: newcomment,
        cid: data.id
      }, res => {
        console.log(res, 'aabcc')
        if (res.data.issuccess == 1) {
          that.setData({ newcomment: '' ,sendshow: !0}, () => {
            that.getdata(cid) //刷新列表
          })
        } else if (res.data.msg = "wrong 包含敏感内容") {
          wx.showToast({
            title: '您输入的评论包含敏感内容，请重新输入',
            icon: 'none'
          })
        }
      })
    }  
  },

  /**
   * 取到用户名和头像
   */
  getuser() {
    wx.getUserInfo({
      success: res => {
        let {
          nickName,
          avatarUrl
        } = res.userInfo
        that.setData({
          nickName,
          avatarUrl
        })
      }
    })

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let {
      total,
      list,
      pagesize
    } = that.data;
    if (list.length < total) { //继续加载评论
      api.resp_mp_getlist({
        cid: that.data.cid,
        pagesize,
        start: list.length
      }, res => {
        if (res.data.issuccess == 1) {
          let newlist = res.data.list;
          newlist.map((x) => {
            x.updateDate = util.myTime(x.updateDate)
          })
          that.setData({
            total,
            list: list.concat(newlist)
          })
          console.log(res.data.list, 'mmlml')
        }
      })

    } else {
      wx.showToast({
        title: '再扯也没有了',
        icon: 'none'
      })

    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: app.logininfo.appletname,
      path: `pages/sharedetail/sharedetail?share=`+app.logininfo.userid+'&id='+that.data.cid,
    }
  }
})